﻿using DDLParser.constant;
using DDLParser.model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DDLParser.sqlserver
{
    public class SqlServerParser
    {
        public List<ClassSchema> Parse(StreamReader stream)
        {
            List<ClassSchema> result = new List<ClassSchema>();

            /*string wordIteratorFile = @"C:\WordIterator.txt";
            if (File.Exists(wordIteratorFile))
            {
                File.Delete(wordIteratorFile);
            }

            string parseResultFile = @"C:\ParseResultFile.txt";
            if (File.Exists(parseResultFile))
            {
                File.Delete(parseResultFile);
            }

            StreamWriter wordIteratorStream = new StreamWriter(wordIteratorFile);
            StreamWriter parseResultStream = new StreamWriter(parseResultFile);*/

            IDDLIterator iterator = new SqlServerIterator(stream);
            IDDLProcessor processor = new SqlServerProcessor();
            while (iterator.HasNext())
            {
                // get a word for file
                DDLWord word = iterator.Next();
                string iteratorResult = word.Word + " => Line No=" + word.LineNumber + " Offset=" + word.LineOffset;
                //Console.WriteLine(iteratorResult);
                //wordIteratorStream.WriteLine(iteratorResult);

                // process word
                processor.DoWord(word);
            }


            result = processor.GetResult();

            //Console.WriteLine("============== Result ===========");
            //parseResultStream.WriteLine("============== Result ===========");
            //Console.WriteLine("Number of Table: " + result.Count);
            //parseResultStream.WriteLine("Number of Table: " + result.Count);
            int count = 1;
            
            foreach (ClassSchema classResult in result)
            {
                string parseResult = count + ". " + "Table Name : " + classResult.ClassName + "\r\n"+
                    "Number of Attribute :" + classResult.Attributes.Count + "\n";
                    
                    
                //Console.WriteLine(parseResult);
                //parseResultStream.WriteLine(parseResult);
                string attribute,datatype;
                int type,length;
                bool pk,nullable,autoincrement ;
                //string attributeFormat = "FormatAttribute =>Name(DataType,DataLength,pk,Nullable,AutoIncrement)";

                //Console.WriteLine(attributeFormat);
                //parseResultStream.WriteLine(attributeFormat);
                foreach(var list in classResult.Attributes)
                {
                    attribute = list.Name;
                    type = list.DataType;
                    
                    if (type == SqlServerDataType.BOOLEAN)
                    {
                        datatype = SqlServerData.BOOLEAN;
                    }
                    else if (type == SqlServerDataType.BIT)
                    {
                        datatype = SqlServerData.BOOLEAN;
                    }
                    else if (type == SqlServerDataType.STRING)
                    {
                        datatype = SqlServerData.STRING;
                    }
                    else if (type == SqlServerDataType.CHAR)
                    {
                        datatype = SqlServerData.STRING;
                    }
                    else if (type == SqlServerDataType.VARCHAR)
                    {
                        datatype = SqlServerData.STRING;
                    }
                    else if (type == SqlServerDataType.NVARCHAR)
                    {
                        datatype = SqlServerData.STRING;
                    }
                    else if (type == SqlServerDataType.DATETIME)
                    {
                        datatype = SqlServerData.DATETIME;
                    }
                    else if (type == SqlServerDataType.INT)
                    {
                        datatype = SqlServerData.INT;
                    }
                    else if (type == SqlServerDataType.BIGINT)
                    {
                        datatype = SqlServerData.INT;
                    }
                    else if (type == SqlServerDataType.SMALLINT)
                    {
                        datatype = SqlServerData.INT;
                    }
                    else if (type == SqlServerDataType.TINYINT)
                    {
                        datatype = SqlServerData.INT;
                    }
                    else
                    {
                        datatype = null;
                    }
                    length = list.DataLength;
                    pk = list.Pk;
                    nullable = list.Nullable;
                    autoincrement = list.AutoIncrement;
                    
                    string attributeValue = attribute + "(" + datatype + "," + length + "," + pk + "," + nullable +
                        "," + autoincrement + ")";
                    //Console.WriteLine(attributeValue);
                    //parseResultStream.WriteLine(attributeValue);
                    
                    
                }
                //Console.WriteLine();
                //parseResultStream.WriteLine();
                count++;
            }

            /*if (wordIteratorStream != null)
            {
                wordIteratorStream.Close();
                wordIteratorStream.Dispose();
            }

            if (parseResultStream != null)
            {
                parseResultStream.Close();
                parseResultStream.Dispose();
            }*/


            return result;
        }
    }
}
